home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Open Transport / OT1.1 Developer Release / Open Transport SDK / Open Tpt Client Developer / Samples / Internet / OTUdpPitchSample.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  8.2 KB  |  381 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OTUdpPitchSample.cp
  3.  
  4.     Contains:    UDP pitch sample code
  5.  
  6.     Copyright:    © 1993-1995 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. // OT UDP Pitch Test Program (as an SIOW app)
  12.  
  13. #include <QuickDraw.h>
  14. #include <stdio.h>
  15. #include <StdLib.h>
  16. #include <TextUtils.h>
  17. #include <strings.h>
  18. #include <String.h>
  19. #include <Events.h>
  20. #include <Desk.h>
  21. #include <Windows.h>
  22. #include <OpenTransport.h>
  23. #include <OpenTptInternet.h>
  24.  
  25.  
  26. /*******************************************************************************
  27. ** GLOBAL VARIABLES
  28. ********************************************************************************/
  29.  
  30. #define kMaxDataLen 256
  31.  
  32. InetPort    gCatchPort=0;
  33. InetHost    gCatchIpAddr=0;
  34. InetPort      gPitchPort=0;
  35. InetHost    gPitchIpAddr=0;
  36.  
  37. UInt32        gNoOfTimes=0;
  38. UInt16        gBindCompleted = 0;
  39.  
  40. OTEventCode    gCode;
  41. TEndpoint*    gCookie;
  42. OSStatus    gErr;
  43.  
  44. struct InetAddress reqsin, retsin, tmpsin;
  45. char data[kMaxDataLen];
  46. char defaultData[] = "Go Cal, beat Stanford !!!";
  47.  
  48. Boolean    gFirstTime = true;
  49.  
  50. /*******************************************************************************
  51. ** Function Prototypes
  52. ********************************************************************************/
  53.  
  54. void        Inits();
  55. void        CleanUp();
  56. void        Idle();    
  57. void        DoIt();
  58. OSStatus    DoStaticBind(TEndpoint* ep);
  59. OSStatus    DoSend(TEndpoint* ep);
  60.  
  61.  
  62. /*******************************************************************************
  63. **  main function
  64. ********************************************************************************/
  65.  
  66. void main(int, char**) 
  67. {
  68.     char userInput[256];
  69.  
  70.     do 
  71.     {
  72.         Inits();
  73.         DoIt();
  74.         CleanUp();
  75.         
  76.         fprintf(stderr, "Again (y/n)?\n");
  77.         gets(&userInput[0]);
  78.     } while ( userInput[0] == 'y' || userInput[0] == 'Y' );
  79.     
  80.     fprintf(stderr, "Bye\n");
  81.     exit (0);
  82. }
  83.  
  84. /*******************************************************************************
  85. ** Initialize Quickdraw and ASLM
  86. ********************************************************************************/
  87.  
  88. void Inits()
  89. {
  90.     if ( gFirstTime == true )
  91.     {
  92.         InitGraf(&qd.thePort);
  93.         gFirstTime = false;
  94.     }
  95.  
  96.     if ( InitOpenTransport() != kOTNoError )
  97.     {
  98.         fprintf(stderr, "OTUdpPitch: Could not initialize Open Transport, exiting\n");
  99.         exit(1);
  100.     }
  101.     
  102. }
  103.  
  104. /*******************************************************************************
  105. ** Clean up at the end
  106. ********************************************************************************/
  107.  
  108. void CleanUp()
  109. {
  110.     CloseOpenTransport();
  111. }
  112.  
  113. /*******************************************************************************
  114. ** Idle
  115. ********************************************************************************/
  116.  
  117. void Idle()
  118. {
  119.     SystemTask();
  120. }
  121.  
  122. /*******************************************************************************
  123. ** IsPressed
  124. ********************************************************************************/
  125.  
  126. Boolean IsPressed(UInt16 k, KeyMap map)
  127. {
  128.     UInt8* keyMap    = (UInt8*)map;
  129.     return (keyMap[k >> 3] >> (k & 7) & 1);
  130. }
  131.  
  132. /*******************************************************************************
  133. ** CmdKey
  134. ********************************************************************************/
  135.  
  136. Boolean UserAbort()
  137. {
  138.     KeyMap    keyMap;
  139.     
  140.     GetKeys(keyMap);
  141.     if ( IsPressed(0x37, keyMap) && 
  142.         (IsPressed(0x2F, keyMap) || IsPressed(0x41, keyMap)) )
  143.     {
  144.         FlushEvents(everyEvent, 0);    // needed so that "gets" gets not confused later on
  145.         return true;
  146.     }
  147.     else
  148.     {
  149.         return false;
  150.     }
  151. }
  152.  
  153.  
  154. /*******************************************************************************
  155. ** EventHandler
  156. ********************************************************************************/
  157.  
  158. pascal void EventHandler(void*, OTEventCode event, OTResult result, void* cookie)
  159. {
  160.     if ( event == T_BINDCOMPLETE )
  161.     {
  162.         gBindCompleted = 1;
  163.     }
  164.     else if ( event == T_OPENCOMPLETE )
  165.     {
  166.         gErr = (OSStatus) result;
  167.         gCookie = (TEndpoint*) cookie;
  168.         gCode = event;
  169.     }
  170.     return;
  171. }
  172.  
  173. /*******************************************************************************
  174. ** DoIt
  175. ********************************************************************************/
  176.  
  177. void DoIt()
  178. {
  179.     TEndpoint*        ep = NULL;
  180.     TEndpointInfo    info;
  181.     OSStatus        err=0;
  182.     long            myport=0;
  183.     InetHost        myaddr=0;
  184.     char            mystr[255];
  185.  
  186.     myport = 0;
  187.     fprintf(stderr, "What UDP port should I use to send a data ? (enter UDP port number)\n");
  188.     if (gets(mystr) != 0)
  189.     {
  190.         stringtonum(mystr, &myport);
  191.         gPitchPort =(InetPort)  myport;
  192.     }
  193.     myaddr = 0;
  194.     fprintf(stderr, "Where should I send a data ? (enter IP address)\n");
  195.     if (gets(mystr) != 0) 
  196.     {
  197.         if (OTInetStringToHost(mystr, &myaddr) == 0)
  198.         {
  199.             gCatchIpAddr = (InetHost) myaddr;
  200.         }
  201.     }
  202.     myport = 0;
  203.     fprintf(stderr, "To which port should I send a data ? (enter port number)\n");
  204.     if (gets(mystr) != 0)
  205.     {
  206.         stringtonum(mystr, &myport);
  207.         gCatchPort = (InetPort) myport;
  208.     }
  209.     fprintf(stderr, "What should I send ? (enter data string)\n");
  210.     if (gets(data) == 0) 
  211.     {
  212.         strcpy(data, defaultData);
  213.         fprintf(stderr, "send default data: <%s>\n", data);
  214.     }
  215.     myport = 0;
  216.     fprintf(stderr, "How many times should I send this data ?\n");
  217.     if (gets(mystr) != 0) 
  218.     {
  219.         stringtonum(mystr, &myport);
  220.         gNoOfTimes = (UInt16)myport;
  221.     }
  222.     
  223.     OTInetHostToString(gCatchIpAddr, mystr);
  224.     fprintf(stderr, "The program will send <%d> packets to <%s:%d> on port <%d>\n", gNoOfTimes, mystr, gCatchPort, gPitchPort);
  225.     OTInitInetAddress(&reqsin, gPitchPort, (InetHost) 0);
  226.  
  227.     do
  228.     {
  229.         //
  230.         // Now create a UDP
  231.         //
  232. #if 0
  233.         gCode = 0;
  234.         gCookie = NULL;
  235.         gErr = 0;
  236.         err = OTAsyncOpenEndpoint(OTCreateConfiguration(kUDPName), 0, 
  237.                                   &info, EventHandler, 0);
  238.         if ( err == 0 )
  239.         {
  240.             while ( gCode == 0 )
  241.                 OTIdle();
  242.             err = gErr;
  243.         }
  244.         if ( err != 0 )
  245.         {
  246.             ep = NULL;
  247.             fprintf(stderr,"ERROR: OpenEndpoint(\"UDP\") failed with %d\n", err);
  248.             break;
  249.         }
  250.         else
  251.         {
  252.             ep = gCookie;
  253.         }
  254. #else
  255.         ep = OTOpenEndpoint(OTCreateConfiguration(kUDPName), 0, &info, &err);
  256.  
  257.         if ( ep == NULL || err != kOTNoError )
  258.         {
  259.             ep = NULL;
  260.             fprintf(stderr,"ERROR: OpenEndpoint(\"UDP\") failed with %d\n", err);
  261.             break;
  262.         }
  263.         //
  264.         // Install notifier we're going to use for testing
  265.         //
  266.         err = ep->InstallNotifier(EventHandler, 0);
  267.         if ( err != kOTNoError )
  268.         {
  269.             fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
  270.             break;
  271.         }
  272. #endif
  273.         //
  274.         // Try to bind
  275.         // 
  276.         ep->SetAsynchronous();
  277.         err = DoStaticBind(ep);
  278.         if ( err != kOTNoError )
  279.             break;        
  280.  
  281.         ep->SetSynchronous();
  282.  
  283.         while ( gNoOfTimes-- && !UserAbort() )
  284.         {
  285.             err = DoSend(ep);
  286.             if ( err != kOTNoError )
  287.                 break;
  288.             Idle();
  289.         }
  290.         //
  291.         // Try to Unbind
  292.         // 
  293.         err = ep->Unbind();
  294.         if ( err != kOTNoError )
  295.         {
  296.             fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
  297.             break;
  298.         }
  299.     } while (false);
  300.  
  301.     if (ep)
  302.     {
  303.         //
  304.         // Remove notifier
  305.         //
  306.         ep->RemoveNotifier();
  307.         //
  308.         // Get rid of endpoint.
  309.         //
  310.         err = OTCloseProvider(ep);
  311.         if ( err != kOTNoError )
  312.         {
  313.             fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
  314.         }
  315.     }
  316. }
  317.  
  318. /*******************************************************************************
  319. ** DoStaticBind
  320. ********************************************************************************/
  321.  
  322. OSStatus DoStaticBind(TEndpoint* ep)
  323. {
  324.     TBind        req, ret;
  325.     OSStatus    err;
  326.  
  327.     // bind udp to current address and port
  328.     req.addr.len = sizeof(struct InetAddress);
  329.     req.addr.buf = (UInt8*) &reqsin;
  330.     req.qlen = 1;                                        // don't care for udp
  331.     ret.addr.maxlen = sizeof(struct InetAddress);
  332.     ret.addr.buf = (UInt8*) &retsin;
  333.  
  334.     err = ep->Bind(&req, &ret);
  335.     if ( err != kOTNoError )
  336.     {
  337.         fprintf(stderr, "Static Bind returns %d\n", err);
  338.         return err;
  339.     }
  340.     while ( gBindCompleted == 0 )
  341.     {
  342.     }
  343.     gBindCompleted = 0;
  344. //    gPitchIpAddr = reqsin.fPort;
  345.     return err;
  346. }
  347.  
  348. /*******************************************************************************
  349. ** DoSend
  350. ********************************************************************************/
  351.  
  352. OSStatus DoSend(TEndpoint* ep)
  353. {
  354.     OSStatus    err = kOTNoError;
  355.     TUnitData    unitdata;
  356.     char        mystr[255];
  357.  
  358.     // Send a UDP datagram
  359.  
  360.     OTInitInetAddress(&tmpsin, gCatchPort, gCatchIpAddr);
  361.     
  362.     unitdata.addr.len = sizeof(struct InetAddress);
  363.     unitdata.addr.buf = (UInt8*) &tmpsin;
  364.     unitdata.opt.len = 0;
  365.     unitdata.opt.buf = 0;
  366.     unitdata.udata.len = strlen(data);
  367.     unitdata.udata.buf = (UInt8*) data;
  368.     
  369.     err = ep->SndUData( &unitdata);
  370.     if ( err != kOTNoError )
  371.     {
  372.         fprintf(stderr, "SndUData() returns %d\n", err);
  373.     }
  374.     else 
  375.     {
  376.         OTInetHostToString(gCatchIpAddr, mystr);
  377.         fprintf(stderr, "Sent <%s> to <%s:%d>\n", data, mystr, gCatchPort);
  378.     }
  379.     return err;
  380. }
  381.